'use strict';
const db = uniCloud.database()
const dbCmd = db.command
const collection = db.collection('opendb-news-articles')
const favoritecollection = db.collection('opendb-news-favorite')
const news = db.collection('news') // 消息库

exports.main = async (event, context) => {
	if (event.type && event.type == 'zan') {

		if (event.zan_status == true) { // true:点赞。false:取消点赞
			// 以前没有点赞过则添加
			return await favoritecollection.add({
				article_id: event.article_id, // 帖子自身的id
				article_title: event.article_title, //帖子的内容
				user_id: event.user_id,
				create_date: event.create_date,
				is_read: false
			}).then(res => {
				// 更新云端消息
				return collection.doc(event.article_id).update({
					like_count: dbCmd.inc(1)
				}).then(likeres => {
					return {
						code: 0,
						message: '今天的小确幸就是被你喜欢！',
					}
				}).catch(err => {
					return {
						code: 0,
						message: err.message,
					}
				})
			}).catch(err => {
				return {
					code: 0,
					message: err.message,
				}
			})
		} else if (event.zan_status == false) {
			// 删除云端点赞信息
			return await favoritecollection.where({
				article_id: event.article_id, // 文章的id
			}).remove().then(res => {
				// 更新云端消息
				return collection.doc(event.article_id).update({
					like_count: dbCmd.inc(-1)
				}).then(likeres => {
					return {
						code: 0,
						message: '被你喜欢过也是一种小确幸！',
					}
				}).catch(err => {
					return {
						code: 0,
						msg: err.message,
					}
				})
			}).catch(err => {
				return {
					code: 0,
					msg: err.message,
				}
			})
		}
	} else if (event.type && event.type == 'lun') {
		let list = await collection.doc(event.comment.memor_id).get();
		// 首先从消息库从获取之前有无点赞/评论者id
		let news_history = await news.where({
			zaned_uid: event.memor_uid
		}).get()
		let comment = [];
		if (list.data[0].comment) {
			list.data[0].comment.push(event.comment);
			comment = list.data[0].comment;
		} else {
			comment.push(event.comment);
			list.data[0].comment = comment
		}
		let res = await collection.doc(event.comment.memor_id).update({
			comment: comment
		})
		// 添加评论信息到消息库
		if (event.memor_uid != event.comment.uid) { //避免自己评论自己时也受到消息
			if (news_history.data.length > 0) { //如果用户的id被评论过,则在comment数组头部添加
				await news.where({
					zaned_uid: event.memor_uid,
				}).update({
					comment: dbCmd.unshift(event.comment)
				})
			} else { //如果不存在,则创建
				await news.add({
					zaned_uid: event.memor_uid,
					zan: [],
					comment: [event.comment],
				})
			}
		}

		// 被评论者应该也受到消息
		if (event.memor_uid != event.comment.passive_uid) { //避免贴主再次收到评论
			// 首先从消息库从获取之前有无点赞/评论者id
			let news_history_passive = await news.where({
				zaned_uid: event.comment.passive_uid
			}).get()
			// 添加评论信息到消息库
			if (news_history_passive.data.length > 0) { //如果用户的id被评论过,则在comment数组头部添加
				await news.where({
					zaned_uid: event.comment.passive_uid,
				}).update({
					comment: dbCmd.unshift(event.comment)
				})
			} else { //如果不存在,则创建
				await news.add({
					zaned_uid: event.comment.passive_uid,
					zan: [],
					comment: [event.comment],
				})
			}
		}

		return {
			code: 0,
			msg: '评论成功',
			data: list.data
		}
	} else if (event.type && event.type == 'lunDelete') {
		//从帖子中移除评论
		await collection.where({
			_id: event._id,
			comment: event.selectedComment
		}).update({
			comment: dbCmd.pull({
				uid: event.selectedComment.uid, //点赞者id和被点赞帖子id和发布时间都相同
				memor_id: event.selectedComment.memor_id,
				createTime: event.selectedComment.createTime
			})
		})

		let list = await collection.doc(event._id).get();
		//从消息列表中移除评论
		await news.where({
			zaned_uid: event._uid, //被点赞的帖子发布者id
			comment: {
				uid: event.selectedComment.uid
			}
		}).update({
			comment: dbCmd.pull({
				uid: event.selectedComment.uid, //点赞者id和被点赞帖子id和发布时间都相同
				memor_id: event.selectedComment.memor_id,
				createTime: event.selectedComment.createTime
			})
		})
		return {
			code: 0,
			msg: '删除成功',
			data: list.data
		}
	} else if (event.type && event.type == 'fileDelete') {
		//从云存储删除文件
		let res = await uniCloud.deleteFile({
			fileList: event.fileIDList
		});

		//返回数据给客户端
		return res
	} else {
		const res = await collection.add(event)
		return res
	}

};
